<template>
  <div class="wrapper" :class="selected ? 'selected' : ''">
    <div class="left">
      <a-space size="small">
        <a-badge :count="selected ? dialog.count : 0" color="#fca7a7">
          <a-avatar :src="dialog.avatar" :size="40" shape="square" />
        </a-badge>
      </a-space>
    </div>

    <div class="right">
      <div class="line1">
        <p class="title">{{ dialog.title }}</p>
        <p class="time">{{ timeString }}</p>
      </div>

      <div class="line2">
        {{ dialog.subTitle }}
      </div>
    </div>

    <div class="chat-item-delete" @click="handleDelete">
      <DeleteOutlined />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ChatSession } from '@/store/useChatStore';
import { DeleteOutlined } from '@ant-design/icons-vue';
import { PropType } from 'vue';

const emits = defineEmits(['deleteDialog']);

const props = defineProps({
  session: {
    type: Object as PropType<ChatSession>,
    required: true,
  },
  selected: {
    type: Boolean,
    required: true,
  },
});

const dialog = props.session.dialog;
const date = new Date(dialog.timestamp);
const timeString = date.toLocaleTimeString('en-US', {
  hour: '2-digit',
  minute: '2-digit',
});

const handleDelete = () => {
  emits('deleteDialog');
};
</script>

<style scoped lang="scss">
.wrapper {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px;
  cursor: pointer;

  p {
    margin: 0;
  }
}

.selected {
  background-color: rgb(240, 240, 240);
}

.left {
  margin-right: 14px;
  img {
    background-color: #f6f6f6;
    border-radius: 2px;
    width: 100%;
    height: 100%;
  }
}

.right {
  flex: 1;
  overflow: hidden;
}

.line1 {
  display: flex;
  justify-content: space-between;
}

.title {
  font-size: 14px;
  color: #333;
}

.time {
  font-size: 9px;
  color: #d5d5d5;
}

.line2 {
  padding-top: 6px;
  font-size: 10px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  max-width: 100%;
  text-align: left;
}

.chat-item-delete {
  position: relative;
  top: 5px;
  right: -15px;
  transition: all ease 0.3s;
  opacity: 0;
  cursor: pointer;
}

.wrapper:hover > .chat-item-delete {
  opacity: 0.5;
  transform: translateX(-10px);
}

.wrapper:hover > .chat-item-delete:hover {
  opacity: 1;
}
</style>
